from socket import socket, AF_INET, SOCK_STREAM

def start_server():
    server_socket = socket(AF_INET, SOCK_STREAM) #定义TCP socket
    host = '127.0.0.1'
    port = 9999
    server_socket.bind((host, port))  #绑定主机和端口号

    server_socket.listen(5)
    print(f'服务器已启动，监听{host}:{port}')

    client,addr = server_socket.accept()
    print(f'{addr}已连接')
    rec_data = client.recv(1024).decode('utf-8')
    while rec_data != 'bye':
        if rec_data != '':
            print(f'收到的信息是：', rec_data)
        send_data = input(f'请输入你要回复的信息：')
        client.send(send_data.encode('utf-8'))
        if send_data == 'bye':
            break
        rec_data = client.recv(1024).decode('utf-8')
    #关闭socket对象
    client.close()
    server_socket.close()

if __name__ == '__main__':
    start_server()